<template>
  <!-- 赠券方案配置 主页面 -->
  <div class="mt10">
    <app-block>
      <zm-form :model="searchForm" ref="searchForm" inline>
        <zm-form-item prop="name">
          <zm-input
            v-model="searchForm.name"
            placeholder="请输入产品方案搜索"
            class="w200"
          ></zm-input>
        </zm-form-item>
        <zm-form-item prop="type">
          <zm-data-select
            has-all
            all-label="请选择方案类型"
            all-value
            v-model="searchForm.type"
            :list="typeList"
            class="w150"
            clearable
          >
          </zm-data-select>
        </zm-form-item>
        <zm-form-item prop="startTime">
          <zm-date-picker
            :target="searchForm"
            start-key="startTime"
            end-key="endTime"
          />
        </zm-form-item>
        <zm-form-item prop="orderNo">
          <zm-input
            v-model="searchForm.orderNo"
            placeholder="输入赠送卡券销售单号"
            class="w200"
          ></zm-input>
        </zm-form-item>
        <zm-form-item prop="saleOrderNo">
          <zm-input
            v-model="searchForm.saleOrderNo"
            placeholder="输入方案关联销售单号"
            class="w200"
          ></zm-input>
        </zm-form-item>
        <zm-form-item>
          <zm-button @click="onAdd" t="add"></zm-button>
          <zm-button @click="onSearch" t="search"></zm-button>
          <zm-button @click="onReset" t="reset"></zm-button>
        </zm-form-item>
        <br />
        <zm-form-item label="方案状态：" prop="status">
          <zm-data-radio
            is-btn
            :list="typeStatus"
            v-model="searchForm.status"
            @change="onSearch"
          />
        </zm-form-item>
      </zm-form>
      <zm-table-data
        :data="tableParams.data"
        :columns="columns"
        :fetch="loadList"
        :table-params="tableParams"
        :show-page="true"
      >
      </zm-table-data>
    </app-block>
    <detail-sendCoupon
      v-if="detailParams.visible"
      v-model="detailParams.visible"
      :obj="detailParams.obj"
      :id="detailParams.id"
      @toEdit="onEdit"
    ></detail-sendCoupon>
    <new-panel
      v-if="newParams.visible"
      v-model="newParams.visible"
      :obj="newParams.obj"
      @refresh="loadList"
    ></new-panel>
  </div>
</template>

<script>
import tableMixin from '@/mixins/zmTableMixin'
import { typeList, typeStatus, columns } from './const'
import sendCouponSetApis from '@/api/src/sendCouponSet'
import DetailSendCoupon from './components/detail'
import NewPanel from './components/New'
export default {
  name: 'PlanConfig',
  components: { DetailSendCoupon, NewPanel },
  mixins: [tableMixin],
  data () {
    return {
      typeList,
      typeStatus,

      searchForm: {
        name: '', // 产品方案名称
        orderNo: '', // 销售订单号
        saleOrderNo: '', // 方案关联销售单号
        startTime: '', // 开始时间
        endTime: '', // 结束时间
        type: '', // 1 根据销售单绑定卡券 2 根据会议支付订单数 3 根据会议购票数量
        status: '1' // 方案状态：1 生效中   2 待生效   3 已失效
      },

      detailParams: {
        visible: false,
        id: '',
        obj: {}
      },
      newParams: {
        visible: false,
        obj: {}
      }
    }
  },
  computed: {
    columns () {
      return columns(this)
    }
  },
  watch: {},
  created () {
  },
  methods: {
    async loadList () {
      const _res = await sendCouponSetApis.getPageList(this.assignQuery(this.searchForm))
      this.initialTableData(_res.list, _res.count)
    },
    onAdd () {
      this.newParams = {
        visible: true,
        obj: {}
      }
    },
    // 新增/编辑
    onEdit (row) {
      this.newParams = {
        visible: true,
        obj: row
      }
    },
    // 详情
    onDetail (row) {
      this.detailParams = {
        visible: true,
        obj: row,
        id: row.id
      }
    },
    // 删除
    async onDel (row) {
      let currentTime = new Date().getTime()
      let startTime = new Date(row.startTime).getTime()
      let endTime = new Date(row.endTime).getTime()
      if (currentTime >= startTime && currentTime <= endTime) {
        this.$confirm('确定删除生效中的方案，方案将不会对销售单生效', '删除', {
          type: 'warning'
        }).then(async () => {
          await sendCouponSetApis.delete({ id: row.id })
          this.onSearch()
          this.$message.success('操作成功')
        })
      } else {
        await sendCouponSetApis.delete({ id: row.id })
        this.onSearch()
        this.$message.success('操作成功')
      }
    }
  }
}
</script>

<style scoped lang="scss"></style>
